<template>
    <div class="loginMain">
        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span>登录</span>
                <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
            </div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="userName">
                    <el-input v-model="ruleForm.userName" size="mini"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="ruleForm.password" :show-password="true" size="mini"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')" size="mini">登录</el-button>
                    <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    import axios from '../util/request';
    export default {
        name: 'CodeUILogin',

        data() {
            return {
                ruleForm: {
                    userName: '',
                    password: ''
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ]
                }
            };
        },

        mounted() {

        },

        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post("/login", this.ruleForm).then(m => {
                            localStorage.setItem("token", m.data.data.token);

                            //从数据库获取菜单数据  axios
                            let menuData = [
                                {
                                    menuId: 2000,
                                    menuName: '菜单管理',
                                    RouteName: 'menu',
                                    route: '/home/menulist',
                                    ComponentPath: '@/views/Menu/MenuList'
                                },
                                {
                                    menuId: 2001,
                                    menuName: '角色管理',
                                    RouteName: 'role',
                                    route: '/home/rolelist',
                                    ComponentPath: '@/views/Role/RoleList'
                                }
                            ];

                            
                            localStorage.setItem("menuData",JSON.stringify(menuData));


                            this.$router.push('/home')
                        });
                    } else {
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        },
    };
</script>

<style scoped>
    .loginMain {
        background: url('../assets/bg.jpg');
        background-size: 100% 100%;
        height: 100%;
        overflow: hidden;
    }

    .box-card {
        width: 30%;
        margin: 0 auto;
        margin-top: 11em;
    }
</style>